package com.lq.ecoldchain.ui.main.orderlist.order.detail

import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lq.ecoldchain.R
import com.lq.ecoldchain.base.BaseNavigator
import com.lq.ecoldchain.base.view.BaseActivity
import com.lq.ecoldchain.databinding.ActivityHandleOrderDetailBinding
import com.lq.ecoldchain.entity.OrderDetailBean
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance

/*
*2019/1/17 0017  14:11
*function by lq
*/
@Route(path = BaseNavigator.handleOrderDetail)
class HandleOrderDetailActivity : BaseActivity<ActivityHandleOrderDetailBinding>() {
    override val layoutId = R.layout.activity_handle_order_detail
    override val viewDelegate: HandleOrderDetailDelegate by instance()

    @Autowired
    @JvmField
    var bean: OrderDetailBean? = null

    @Autowired
    @JvmField
    var id: String? = ""

    override fun initView() {
        ARouter.getInstance().inject(this)
        titleText("订单详情")
        binding.delegate = viewDelegate
        binding.bean = bean
        BaseNetCore.ApiServiceImpl.orderDetail(id!!.toInt()).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<OrderDetailBean>() {
            override fun onSuccess(t: OrderDetailBean?) {
                bean = t
                binding.bean = t
                //do  判断当前状态为装货卸货  5是卸货
                val list = t!!.cargoHandling.sortedBy {
                    it.sort
                }
                if (list.isEmpty()) {
                    toast { "请联系客服完善信息" }
                } else {
                    val currentStatus = if (t.status == 2) {
                        1
                    } else {
                        2
                    }
                    for (item in list) {
                        if (item.time == null && item.type == currentStatus) {
                            viewDelegate.viewModel.id = item.id
                            return
                        }
                    }
                }
            }

            override fun onFail(code: String) {}
        })
    }

    override val kodein = Kodein.lazy {
        extend(parentKodein)
        import(handleOrderDetailKodeinModule)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            CameraUtil.handlePhoto(requestCode, data?.data, null, object : QiNiuUtils.OnQiNiuListener {
                override fun uploadSuccess(url: String) {
                    viewDelegate.viewModel.add(url)
                    logi { url }
                }
            })
        }
    }

}